package vsy.buzzwordbingo.login;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import vsy.buzzwordbingo.model.DuplicatePlayerException;
import vsy.buzzwordbingo.model.GameCore;

public class PlayerAuthenticationProcessingFilter extends
		UsernamePasswordAuthenticationFilter {

	@Resource
	private GameCore gameCore;
	
	@Override
	public Authentication attemptAuthentication(HttpServletRequest request,
			HttpServletResponse response) throws AuthenticationException {
		String playerName = request.getParameter("j_username");
		String password = request.getParameter("j_password");
		try {
			gameCore.loginPlayer(playerName, password);
		} catch (DuplicatePlayerException e) {
		}
		return super.attemptAuthentication(request, response);
	}
}